package com.ctl.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
        CountDownLatch countDownLatch=new CountDownLatch(10);
        ReentrantLock lock = new ReentrantLock(false);
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            service.execute(new Thread(()->{
                lock.lock();
//                try {
//                    Thread.sleep(new Random().nextInt(1000));
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                System.out.println(finalI);
                countDownLatch.countDown();
                lock.unlock();
            }));
        }

        countDownLatch.await();
        service.shutdown();
    }
}
